#include "vlc_player.h"
#include <QDebug>
#include <QFileDialog>
#include <QInputDialog>
#include <QPushButton>
#include <QVBoxLayout>
#include <VLCQtCore/Audio.h>
#include <VLCQtCore/Common.h>
#include <VLCQtCore/Instance.h>
#include <VLCQtCore/Media.h>
#include <VLCQtCore/MediaPlayer.h>
#include <VLCQtWidgets/WidgetVideo.h>
#include "vodlistewgt.h"

VLC_Player::VLC_Player(QWidget *parent)
    : QWidget(parent)
    ,_media(nullptr)
    ,curUrl("")
{
    setFixedSize(800,500);
    QVBoxLayout* layout = new QVBoxLayout(this);

    //创建播放窗口
    _video = new VlcWidgetVideo(this);
    _vlcInstance = new VlcInstance(VlcCommon::args(),this);
    _player = new VlcMediaPlayer(_vlcInstance);
    _audio = new VlcAudio(_player);

    //设置默认音量大小
    _audio->setVolume(50);
    _player->setVideoWidget(_video);
    _video->setMediaPlayer(_player);

    //连接vlcmediaplayer的timechanged,更新进度条
    connect(_player,&VlcMediaPlayer::timeChanged,this,[this](int value){
       emit TimeChanged(value);
    });

    _voidlistWgt = new VodListWgt (this);
    //创建窗口分割器
    _spliter = new QSplitter (Qt::Horizontal,this);
    _spliter->addWidget(_video);
    _spliter->addWidget(_voidlistWgt);
    //设置窗口占比
    int totalWidth = width();
    _spliter->setSizes({totalWidth * 4 / 5,totalWidth / 5});
    //将分割器添加到主布局

    layout->addWidget(_spliter);
    setLayout(layout);

}
//vlc_qt库
VLC_Player::~VLC_Player()
{
    delete  _audio;
    delete  _player;
    delete  _media;
    delete  _vlcInstance;
}

void VLC_Player::OpenUrl(const QString url)
{
    if(url.isEmpty())
    {
        return;
    }
    qDebug() <<"url:"<<url;
    curUrl = url;
    _media = new VlcMedia(url,true,_vlcInstance);
    //解析媒体元数据
    _media->parse();

    connect(_media,SIGNAL(parsedChanged(bool)),this,SLOT(onParsedChanged(bool)),Qt::QueuedConnection);
}

void VLC_Player::onParsedChanged(bool status)
{
    if(status)
    {
        QVariantMap map;
        map["duration"] = _media->duration();
        map["volum"] = 50;
        emit MediaInfo(map);
        _player->open(_media);
    }

}

void VLC_Player::onBuffering(float newCache)
{
    //播放流畅处理
    if(isSeeking && newCache >= 1.0f)
    {
        isSeeking = false;
        _player->play();
    }
}

void VLC_Player::HandleControlSignal(const QVariant &data)
{
    if(!_player)return;
    Vlc::State state = _player->state();

    if(data.type() == QVariant::String)
    {
        QString action = data.toString();
        if(action == "play")
        {
            if(state == Vlc::State::Paused)
            {
                _player->resume();
            }
            else if(state == Vlc::State::Stopped)
            {
                OpenUrl(curUrl);
            }
        }
        else if(action == "pause")
        {
            if(state == Vlc::State::Playing)
            {
                _player->pause();
            }
        }
        else if(action == "stop")
        {
            if(state == Vlc::Playing || state == Vlc::Stopped || state == Vlc::Paused)
            {
                _player->stop();
            }
        }
    }
}
